package com.kalemao.library.utils.upload;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;

/**
 * Created by guoli on 2016/7/4.
 */
public class UploadFileRequestBody extends RequestBody {

    private RequestBody      mRequestBody;
    private ProgressListener mProgressListener;

    private BufferedSink     bufferedSink;

    public UploadFileRequestBody(File file, ProgressListener progressListener) {
        this.mRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        this.mProgressListener = progressListener;
    }

    public UploadFileRequestBody(byte[] mByte, ProgressListener progressListener) {
        this.mRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), mByte);
        this.mProgressListener = progressListener;
    }

    public UploadFileRequestBody(RequestBody requestBody, ProgressListener progressListener) {
        this.mRequestBody = requestBody;
        this.mProgressListener = progressListener;
    }

    @Override
    public MediaType contentType() {
        return mRequestBody.contentType();
    }

    @Override
    public long contentLength() {
        try {
            return mRequestBody.contentLength();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void writeTo(BufferedSink sink) {
        if (bufferedSink == null) {
            // 包装
            bufferedSink = Okio.buffer(sink(sink));
        }
        try {
            // 写入
            mRequestBody.writeTo(bufferedSink);
            // 必须调用flush，否则最后一部分数据可能不会被写入
            bufferedSink.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Sink sink(Sink sink) {
        return new ForwardingSink(sink) {
            // 当前写入字节数
            long bytesWritten  = 0L;
            // 总字节长度，避免多次调用contentLength()方法
            long contentLength = 0L;

            @Override
            public void write(Buffer source, long byteCount) throws IOException {
                super.write(source, byteCount);
                if (contentLength == 0) {
                    // 获得contentLength的值，后续不再调用
                    contentLength = contentLength();
                }
                // 增加当前写入的字节数
                bytesWritten += byteCount;
                // 回调
                if (mProgressListener != null)
                    mProgressListener.onProgress(bytesWritten, contentLength, bytesWritten == contentLength);
            }
        };
    }
}
